package com.cz.background.controller.common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.cz.background.common.exception.BizException;
import com.cz.background.dto.common.menu.AddSysMenuDTO;
import com.cz.background.dto.common.menu.MenuQueryDTO;
import com.cz.background.dto.common.menu.UpdateStatusDTO;
import com.cz.background.dto.common.menu.UpdateSysMenuDTO;
import com.cz.background.entity.common.SysMenu;
import com.cz.background.service.common.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/menu")
@Tag(name = "菜单管理")
public class MenuController {

    @Autowired
    private SysMenuService menuService;

    @PostMapping("tree")
    @Operation(summary = "查询菜单树")
    public List<Tree<String>> queryMenuTree(@RequestBody(required = false) MenuQueryDTO menuQueryDTO) {
        List<Tree<String>> menuTreeList = new ArrayList<>();
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        List<SysMenu> sysMenuEntityList = menuService.queryMenuTree(menuQueryDTO);
        if (CollUtil.isNotEmpty(sysMenuEntityList)) {
            sysMenuEntityList.forEach(menu -> {
                TreeNode<String> treeNode = new TreeNode<>(menu.getId(), menu.getParentId(), menu.getName(), 0);
                Map<String, Object> extra = new HashMap<>();
                extra.put("url", menu.getUrl());
                extra.put("status", menu.getStatus());
                treeNode.setExtra(extra);
                nodeList.add(treeNode);
            });
            menuTreeList = TreeUtil.build(nodeList, "0");
        }

        return menuTreeList;
    }

    @PostMapping("tree/all")
    @Operation(summary = "查询所有菜单树")
    public List<Tree<String>> queryAllMenuTree() {
        List<Tree<String>> menuTreeList = new ArrayList<>();
        List<SysMenu> menuList = menuService.queryMenuTree(null);
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        if (CollUtil.isNotEmpty(menuList)) {
            menuList.forEach(menu -> {
                TreeNode<String> treeNode = new TreeNode<>(menu.getId(), menu.getParentId(), menu.getName(), 0);
                Map<String, Object> extra = new HashMap<>();
                extra.put("url", menu.getUrl());
                extra.put("status", menu.getStatus());
                treeNode.setExtra(extra);
                nodeList.add(treeNode);
            });
            menuTreeList = TreeUtil.build(nodeList, "-1");
        }
        return menuTreeList;
    }

    @PostMapping("delMenu")
    @Operation(summary = "删除用户")
    public void delMenu(@RequestBody List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw new BizException("ids不能为空");
        }
        menuService.delMenu(ids);
    }

    @PostMapping("addMenu")
    @Operation(summary = "添加菜单")
    public void addMenu(@RequestBody @Valid AddSysMenuDTO addSysMenuDTO) {
        menuService.addMenu(addSysMenuDTO);
    }

    @PostMapping("updateMenu")
    @Operation(summary = "修改菜单")
    public void updateMenu(@RequestBody @Valid UpdateSysMenuDTO updateSysMenuDTO) {
        menuService.updateMenu(updateSysMenuDTO);
    }

    @PostMapping("updateMenuStatus")
    @Operation(summary = "修改菜单")
    public void updateMenuStatus(@RequestBody @Valid UpdateStatusDTO updateStatusDTO) {
        menuService.updateStatus(updateStatusDTO);
    }
}
